package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionDXT

import android.content.Context
import android.util.Log
import android.view.View
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseFragment
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkDialog.ShowDialogTools
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.JumpActivityHelp
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod.addClickScale
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseFragmentPagerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.PaperEvaluationInfor
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.PaperEvaluationInforIndexHelp
import com.lancoo.znbkxx.teacher.utils.WebStyleUtils
import kotlinx.android.synthetic.main.aibk_fragment_model_dxt.view.*
import kotlinx.android.synthetic.main.aibk_fragment_model_dxt.view.IncludeQuesInformationView
import kotlinx.android.synthetic.main.aibk_fragment_model_dxt.view.WvAsk
import kotlinx.android.synthetic.main.aibk_include_layout_evaluation_button_view.view.*
import kotlinx.android.synthetic.main.aibk_include_ques_assessment.view.*
import kotlinx.android.synthetic.main.aibk_include_question_information_view.view.*
import java.util.ArrayList

class EvaluationFragmentDxt(
        val activity: AppCompatActivity,
        val paperEvaluationInfor: PaperEvaluationInfor,
        val index_A: Int,
        val index_B: Int
) : KBaseFragment(), View.OnClickListener {

    private var rootView: View? = null
    private var fragmentList: MutableList<Fragment>? = ArrayList()

    override fun createLayout(): Int {
        return R.layout.aibk_fragment_model_dxt
    }

    override fun initView(view: View?) {
        rootView = view
        showQuestionView()
    }

    private fun showQuestionView() {

        rootView!!.RlEvaluationButtonView.visibility = View.VISIBLE
        rootView!!.BtnEvaluationQues.setOnClickListener(this)
        rootView!!.BtnEvaluationQues.addClickScale()
        //隐藏单题评估系数
        rootView!!.RlQuesAssessment.visibility = View.GONE

        //是否显示来源
        var resource = paperEvaluationInfor.TypeList[index_A].QuesDetailList[index_B].Resource
        if (resource.isEmpty()) {
            rootView!!.IncludeQuesInformationView.TvSourceFrom.visibility = View.INVISIBLE
        } else {
            rootView!!.IncludeQuesInformationView.TvSourceFrom.visibility = View.VISIBLE
            rootView!!.IncludeQuesInformationView.TvSourceFrom.text = "来源: $resource"
        }
        //显示试题准确率
        var quesScoreRate = paperEvaluationInfor.TypeList[index_A].QuesList[index_B].ScoreRate
        if (quesScoreRate != "--") {
            quesScoreRate = "$quesScoreRate%"
        }
        rootView!!.IncludeQuesInformationView.TvScoreRate.text = "得分率: $quesScoreRate"

        var itemSortIndex = PaperEvaluationInforIndexHelp.getChildListBean(paperEvaluationInfor, index_A, index_B, 0).ItemList[0].ItemSortIndex
        var childAsk = PaperEvaluationInforIndexHelp.getChildListBean(paperEvaluationInfor, index_A, index_B, 0).ChildAsk
        childAsk = "$itemSortIndex. $childAsk"
        if (WebStyleUtils.hasTable(childAsk)) {
            WebStyleUtils.showWebViewCssStyle(activity, rootView!!.WvAsk, childAsk, false)
        } else {
            WebStyleUtils.showWebViewCssStyle(activity,  rootView!!.WvAsk, childAsk, true)
        }
        rootView!!.WvAsk.addJavascriptInterface(
                JSInterface(
                        activity,
                        WebStyleUtils.returnImageUrlsFromHtml(childAsk)
                ), "Android"
        )
        rootView!!.WvAsk.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
                WebStyleUtils.setWebImageClick(view, "Android")
            }
        }

        //加载试题详情
        fragmentList!!.clear()
        (fragmentList as ArrayList<Fragment>).add(
                EvaluationFragmentParentDxt(activity, paperEvaluationInfor,index_A,index_B)
        )
        val mAdapter = KBaseFragmentPagerAdapter(childFragmentManager, fragmentList as ArrayList<Fragment>)
        rootView!!.VpAnswer.adapter = mAdapter
    }

    //JS点击事件处理
    inner class JSInterface(var context: Context, var imageUrls: Array<String>?) {

        @JavascriptInterface
        fun clickFunction(position: String) {
            return
        }

        //java回调js代码，不要忘了@JavascriptInterface这个注解，不然点击事件不起作用
        @JavascriptInterface
        fun openImage(img: String, pos: Int) {
            val paths = ArrayList<String>()
            paths.add(img)
            Log.e("openImage", "$pos========点击了图片=========$img")
            ShowDialogTools.showBigImageDialog(activity, img)
        }
    }

    override fun onClick(v: View?) {
        when(v?.id){
            R.id.BtnEvaluationQues ->{
                MyToast.showToast("跳转到教师端讲评界面--单选题")
//                JumpActivityHelp.goToCommentDetailActivity(activity,paperEvaluationInfor,index_A,index_B)
            }
        }
    }

}